#pragma once

#include <QtGui>

class Filter
        : public QObject
{
    bool eventFilter(QObject *obj, QEvent *event)
    {
        if(event->type() == QEvent::KeyPress) {
            QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
            if(keyEvent->key() >= Qt::Key_A && keyEvent->key() <= Qt::Key_Z &&
                ((keyEvent->modifiers() & Qt::ShiftModifier) == false))
            {
                QApplication::sendEvent(obj, new QKeyEvent(QEvent::KeyPress,
                    keyEvent->key(), keyEvent->modifiers() | Qt::ShiftModifier,
                    keyEvent->text().toUpper()));
                QApplication::sendEvent(obj, new QKeyEvent(QEvent::KeyRelease,
                    keyEvent->key(), keyEvent->modifiers() | Qt::ShiftModifier,
                    keyEvent->text().toUpper()));
                return true;
            }
        }

        return QObject::eventFilter(obj, event);
    }
};


class uppercase_lineedit
        : public QLineEdit
{
    Q_OBJECT

public:
    uppercase_lineedit(QWidget *parent = nullptr)
        : QLineEdit(parent)
    {
        installEventFilter(&filter_);
//        connect(this, SIGNAL(textChanged(const QString&)), this, SLOT(on_text_changed_slot(const QString&)));
    }

    uppercase_lineedit(const QString &contents, QWidget *parent = nullptr)
        : QLineEdit(contents, parent)
    {
        installEventFilter(&filter_);
//        connect(this, SIGNAL(textChanged(const QString&)), this, SLOT(on_text_changed_slot(const QString&)));
    }

//private Q_SLOTS:
//    void on_text_changed_slot(const QString &text)
//    {
//        disconnect(this, SIGNAL(textChanged(const QString&)), this, SLOT(on_text_changed_slot(const QString&)));
//        setText(text.toUpper());
//        connect(this, SIGNAL(textChanged(const QString&)), this, SLOT(on_text_changed_slot(const QString&)));
//    }

private:
    Filter filter_;
};